<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { Delete, Edit, Plus, Search, Refresh, User } from '@element-plus/icons-vue'
import type {
  Role,
  User as UserType,
  UserQueryRequest,
  UserRoleVO,
} from '@/api-client/src/api-client/models'
import { getRoleController } from '@/api-client/src/api-client/role-controller/role-controller'
import { getUserController } from '@/api-client/src/api-client/user-controller/user-controller'

// API控制器实例
const roleApi = getRoleController()
const userApi = getUserController()

// 响应式状态
const loading = ref(false)
const userLoading = ref(false)
const roleList = ref<Role[]>([])
const userList = ref<UserType[]>([])
const userRoleList = ref<UserRoleVO[]>([])
const total = ref(0)
// 改为支持一个用户多个角色的映射
const userRoleMap = ref<Map<number, Role[]>>(new Map())

// 搜索表单
const searchForm = reactive<UserQueryRequest>({
  current: 1,
  pageSize: 10,
  userName: '',
  type: undefined,
})

// --- 角色对话框状态 ---
const roleDialogVisible = ref(false)
const isEditRole = ref(false)
const roleDialogTitle = computed(() => (isEditRole.value ? '编辑角色' : '新增角色'))
const roleFormRef = ref<FormInstance>()

// 角色表单
const roleForm = ref<Partial<Role>>({
  rid: undefined,
  rname: '',
  remark: '',
})

// 角色表单校验规则
const roleRules = reactive<FormRules<typeof roleForm>>({
  rname: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
})

// --- 用户角色分配对话框状态 ---
const userRoleDialogVisible = ref(false)
const selectedUser = ref<UserType | null>(null)
const selectedRoleId = ref<number | undefined>(undefined)

// --- 用户角色详情对话框状态 ---
const userRoleDetailDialogVisible = ref(false)
const selectedUserForDetail = ref<UserType | null>(null)

// 获取角色列表
const fetchRoles = async () => {
  try {
    const res = await roleApi.getAllRoles()
    if (res.code === 200 && res.data) {
      roleList.value = res.data
    } else {
      ElMessage.error(res.message || '获取角色列表失败')
    }
  } catch {
    ElMessage.error('网络错误，请稍后再试')
  }
}

// 获取用户列表
const fetchUsers = async () => {
  userLoading.value = true
  try {
    const res = await userApi.queryUserByPage(searchForm)
    if (res.code === 200 && res.data) {
      userList.value = res.data.records || []
      total.value = res.data.total || 0
    } else {
      ElMessage.error(res.message || '获取用户列表失败')
    }
  } catch {
    ElMessage.error('网络错误，请稍后再试')
  } finally {
    userLoading.value = false
  }
}

// 获取用户角色关联信息
const fetchUserRoles = async () => {
  try {
    const res = await roleApi.listUserRole()
    if (res.code === 200 && res.data) {
      userRoleList.value = res.data
      // 更新用户角色映射
      updateUserRoleMap()
    } else {
      ElMessage.error(res.message || '获取用户角色信息失败')
    }
  } catch {
    ElMessage.error('获取用户角色信息失败')
  }
}

// 更新用户角色映射 - 支持一个用户多个角色
const updateUserRoleMap = () => {
  userRoleMap.value.clear()
  userRoleList.value.forEach((userRole) => {
    if (userRole.userid && userRole.roleId && userRole.roleName) {
      const role: Role = {
        rid: userRole.roleId,
        rname: userRole.roleName,
        remark: '',
      }

      // 如果用户已存在，添加到现有角色数组中
      const existingRoles = userRoleMap.value.get(userRole.userid) || []
      // 检查是否已经存在相同角色，避免重复
      const roleExists = existingRoles.some((r) => r.rid === role.rid)
      if (!roleExists) {
        existingRoles.push(role)
        userRoleMap.value.set(userRole.userid, existingRoles)
      }
    }
  })
}

// 组件挂载时获取初始数据
onMounted(async () => {
  await fetchRoles()
  await fetchUsers()
  await fetchUserRoles()
})

/**
 * 处理搜索
 */
const handleSearch = () => {
  searchForm.current = 1
  fetchUsers()
}

/**
 * 处理重置搜索表单
 */
const handleReset = () => {
  searchForm.userName = ''
  searchForm.type = undefined
  searchForm.current = 1
  fetchUsers()
}

/**
 * 处理分页变化
 */
const handleCurrentChange = (page: number) => {
  searchForm.current = page
  fetchUsers()
}

const handleSizeChange = (size: number) => {
  searchForm.pageSize = size
  searchForm.current = 1
  fetchUsers()
}

/**
 * 打开新增角色对话框
 */
const handleAddRole = () => {
  isEditRole.value = false
  roleForm.value = {
    rid: undefined,
    rname: '',
    remark: '',
  }
  roleDialogVisible.value = true
}

/**
 * 打开编辑角色对话框
 */
const handleEditRole = (role: Role) => {
  isEditRole.value = true
  roleForm.value = { ...role }
  roleDialogVisible.value = true
}

/**
 * 保存角色
 */
const handleSaveRole = async () => {
  if (!roleFormRef.value) return

  try {
    await roleFormRef.value.validate()
    loading.value = true

    // 构建角色对象，确保必要字段存在
    const roleData: Role = {
      rid: isEditRole.value ? roleForm.value.rid : undefined,
      rname: roleForm.value.rname || '',
      remark: roleForm.value.remark || '',
    }
    console.log(roleData)
    let res
    if (isEditRole.value) {
      res = await roleApi.updateRole(roleData)
    } else {
      res = await roleApi.addRole(roleData)
    }
    if (res.code === 200) {
      ElMessage.success(isEditRole.value ? '角色更新成功' : '角色添加成功')
      roleDialogVisible.value = false
      await fetchRoles()
      // 如果是添加角色后，刷新用户角色信息
      if (!isEditRole.value) {
        await fetchUserRoles()
      }
    } else {
      ElMessage.error(res.message || '操作失败')
    }
  } catch (error) {
    console.error('保存角色失败:', error)
    ElMessage.error('请检查输入信息')
  } finally {
    loading.value = false
  }
}

/**
 * 删除角色
 */
const handleDeleteRole = async (role: Role) => {
  if (!role.rid) return

  try {
    await ElMessageBox.confirm(`确定要删除角色"${role.rname}"吗？`, '删除确认', {
      type: 'warning',
    })

    loading.value = true
    const res = await roleApi.deleteRole({ id: role.rid })
    if (res.code === 200) {
      ElMessage.success('角色删除成功')
      await fetchRoles()
    } else {
      ElMessage.error(res.message || '删除失败')
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  } finally {
    loading.value = false
  }
}

/**
 * 打开用户角色分配对话框
 */
const handleAssignRole = (user: UserType) => {
  selectedUser.value = user
  selectedRoleId.value = undefined
  userRoleDialogVisible.value = true
}

/**
 * 分配用户角色
 */
const handleSaveUserRole = async () => {
  if (!selectedUser.value?.id || !selectedRoleId.value) {
    ElMessage.error('请选择角色')
    return
  }

  try {
    loading.value = true
    const res = await roleApi.addUserRole({
      userId: selectedUser.value.id,
      roleId: selectedRoleId.value,
    })

    if (res.code === 200) {
      ElMessage.success('角色分配成功')
      userRoleDialogVisible.value = false

      // 刷新用户角色信息
      await fetchUserRoles()
      await fetchUsers()
    } else {
      ElMessage.error(res.message || '角色分配失败')
    }
  } catch {
    ElMessage.error('网络错误，请稍后再试')
  } finally {
    loading.value = false
  }
}

/**
 * 获取用户类型文本
 */
const getUserTypeText = (type?: number) => {
  switch (type) {
    case 1:
      return '管理员'
    case 2:
      return '普通用户'
    default:
      return '未知'
  }
}

// 打开用户角色详情对话框
const handleViewUserRoles = (user: UserType) => {
  selectedUserForDetail.value = user
  userRoleDetailDialogVisible.value = true
}

// 移除指定用户角色
const handleRemoveSpecificUserRole = async (user: UserType, role: Role) => {
  if (!user.id || !role.rid) return

  try {
    await ElMessageBox.confirm(
      `确定要移除用户"${user.userName}"的角色"${role.rname}"吗？`,
      '移除角色确认',
      {
        type: 'warning',
        confirmButtonText: '确定移除',
        cancelButtonText: '取消',
      },
    )

    loading.value = true
    const res = await roleApi.deleteUserRole({ userId: user.id, roleId: role.rid })

    if (res.code === 200) {
      ElMessage.success('角色移除成功')
      // 刷新用户角色信息
      await fetchUserRoles()
      await fetchUsers()
    } else {
      ElMessage.error(res.message || '移除角色失败')
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('移除角色失败')
    }
  } finally {
    loading.value = false
  }
}

// 移除用户的所有角色
const handleRemoveAllUserRoles = async (user: UserType) => {
  if (!user.id) return

  const userRoles = userRoleMap.value.get(user.id) || []
  if (userRoles.length === 0) {
    ElMessage.info('该用户暂无角色')
    return
  }

  try {
    await ElMessageBox.confirm(
      `确定要移除用户"${user.userName}"的所有角色吗？`,
      '移除所有角色确认',
      {
        type: 'warning',
        confirmButtonText: '确定移除',
        cancelButtonText: '取消',
      },
    )

    loading.value = true

    // 批量移除所有角色
    const promises = userRoles.map((role) =>
      roleApi.deleteUserRole({ userId: user.id!, roleId: role.rid! }),
    )

    const results = await Promise.all(promises)
    const successCount = results.filter((res) => res.code === 200).length

    if (successCount === userRoles.length) {
      ElMessage.success(`成功移除所有${userRoles.length}个角色`)
    } else {
      ElMessage.warning(`移除了${successCount}个角色，${userRoles.length - successCount}个失败`)
    }

    // 刷新用户角色信息
    await fetchUserRoles()
    await fetchUsers()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('移除角色失败')
    }
  } finally {
    loading.value = false
  }
}

// 在script setup中，所有顶层定义的变量和函数都会自动暴露给模板
</script>

<template>
  <div class="role-view">
    <div class="page-header">
      <h2 class="page-title">角色管理</h2>
      <p class="page-description">管理系统角色和用户角色分配</p>
    </div>

    <el-card class="role-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <div class="header-content">
            <el-icon class="header-icon"><User /></el-icon>
            <span class="card-title">系统角色管理</span>
          </div>
          <el-button type="primary" :icon="Plus" @click="handleAddRole"> 新增角色 </el-button>
        </div>
      </template>

      <el-table
        :data="roleList"
        style="width: 100%"
        v-loading="loading"
        stripe
        :header-cell-style="{ background: '#f8f9fa', color: '#495057' }"
      >
        <el-table-column prop="rid" label="角色ID" width="100" align="center">
          <template #default="{ row }">
            <el-tag size="small" type="info">#{{ row.rid }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="rname" label="角色名称" min-width="150">
          <template #default="{ row }">
            <div class="role-name">
              <el-icon class="role-icon"><User /></el-icon>
              <span>{{ row.rname }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="角色描述" min-width="200">
          <template #default="{ row }">
            <span class="role-remark">{{ row.remark || '暂无描述' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="{ row }">
            <el-button size="small" type="primary" :icon="Edit" @click="handleEditRole(row)" plain>
              编辑
            </el-button>
            <el-button
              size="small"
              type="danger"
              :icon="Delete"
              @click="handleDeleteRole(row)"
              plain
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div class="divider-section">
      <el-divider>
        <el-icon><User /></el-icon>
        <span style="margin-left: 8px">用户角色分配</span>
      </el-divider>
    </div>

    <el-card class="user-role-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <div class="header-content">
            <el-icon class="header-icon"><Search /></el-icon>
            <span class="card-title">用户角色分配</span>
          </div>
        </div>
      </template>

      <!-- 搜索表单 -->
      <div class="search-section">
        <el-form :model="searchForm" inline class="search-form">
          <el-form-item label="用户名">
            <el-input
              v-model="searchForm.userName"
              placeholder="请输入用户名"
              clearable
              @keyup.enter="handleSearch"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="用户类型">
            <el-select
              v-model="searchForm.type"
              placeholder="请选择用户类型"
              clearable
              style="width: 150px"
            >
              <el-option label="管理员" :value="1" />
              <el-option label="普通用户" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="handleSearch"> 搜索 </el-button>
            <el-button :icon="Refresh" @click="handleReset"> 重置 </el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 用户列表 -->
      <el-table
        :data="userList"
        style="width: 100%"
        v-loading="userLoading"
        stripe
        :header-cell-style="{ background: '#f8f9fa', color: '#495057' }"
      >
        <el-table-column prop="id" label="用户ID" width="100" align="center">
          <template #default="{ row }">
            <el-tag size="small" type="info">#{{ row.id }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="用户名" min-width="120">
          <template #default="{ row }">
            <div class="user-name">
              <el-avatar :size="24" style="margin-right: 8px">
                {{ row.userName?.charAt(0)?.toUpperCase() }}
              </el-avatar>
              <span>{{ row.userName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="用户类型" width="120" align="center">
          <template #default="{ row }">
            <el-tag :type="row.type === 1 ? 'danger' : 'primary'" size="small">
              {{ getUserTypeText(row.type) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="当前角色" min-width="200" align="center">
          <template #default="{ row }">
            <div
              v-if="userRoleMap.get(row.id) && userRoleMap.get(row.id)!.length > 0"
              class="user-roles"
            >
              <el-tag
                v-for="role in userRoleMap.get(row.id)?.slice(0, 2)"
                :key="role.rid"
                type="success"
                size="small"
                effect="light"
                style="margin: 2px"
              >
                <el-icon style="margin-right: 4px"><User /></el-icon>
                {{ role.rname }}
              </el-tag>
              <el-tag
                v-if="userRoleMap.get(row.id)!.length > 2"
                type="info"
                size="small"
                effect="plain"
                style="margin: 2px; cursor: pointer"
                @click="handleViewUserRoles(row)"
              >
                +{{ userRoleMap.get(row.id)!.length - 2 }}个
              </el-tag>
            </div>
            <el-tag v-else type="info" size="small" effect="plain"> 未分配角色 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注" min-width="150">
          <template #default="{ row }">
            <span class="user-remark">{{ row.remarks || '暂无备注' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" align="center">
          <template #default="{ row }">
            <el-button
              size="small"
              type="primary"
              :icon="User"
              @click="handleAssignRole(row)"
              plain
            >
              分配角色
            </el-button>
            <el-button
              v-if="userRoleMap.get(row.id) && userRoleMap.get(row.id)!.length > 0"
              size="small"
              type="info"
              :icon="Search"
              @click="handleViewUserRoles(row)"
              plain
            >
              查看角色
            </el-button>
            <el-button
              v-if="userRoleMap.get(row.id) && userRoleMap.get(row.id)!.length > 0"
              size="small"
              type="warning"
              :icon="Delete"
              @click="handleRemoveAllUserRoles(row)"
              plain
            >
              移除角色
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="searchForm.current"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
        />
      </div>
    </el-card>

    <!-- 角色编辑对话框 -->
    <el-dialog
      v-model="roleDialogVisible"
      :title="roleDialogTitle"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form ref="roleFormRef" :model="roleForm" :rules="roleRules" label-width="80px">
        <el-form-item label="角色名称" prop="rname">
          <el-input v-model="roleForm.rname" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="角色描述" prop="remark">
          <el-input
            v-model="roleForm.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入角色描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="roleDialogVisible = false">取消</el-button>
          <el-button type="primary" :loading="loading" @click="handleSaveRole"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 用户角色分配对话框 -->
    <el-dialog
      v-model="userRoleDialogVisible"
      title="分配用户角色"
      width="450px"
      :close-on-click-modal="false"
    >
      <div v-if="selectedUser">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="用户名">{{ selectedUser.userName }}</el-descriptions-item>
          <el-descriptions-item label="用户类型">
            <el-tag :type="selectedUser.type === 1 ? 'danger' : 'primary'">
              {{ getUserTypeText(selectedUser.type) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="当前角色">
            <div
              v-if="
                selectedUser.id &&
                userRoleMap.get(selectedUser.id) &&
                userRoleMap.get(selectedUser.id)!.length > 0
              "
            >
              <el-tag
                v-for="role in userRoleMap.get(selectedUser.id)"
                :key="role.rid"
                type="success"
                size="small"
                style="margin: 2px"
              >
                {{ role.rname }}
              </el-tag>
            </div>
            <span v-else style="color: #999">未分配角色</span>
          </el-descriptions-item>
        </el-descriptions>

        <el-divider />

        <el-form label-width="80px">
          <el-form-item label="新角色">
            <el-select
              v-model="selectedRoleId"
              placeholder="请选择要分配的角色"
              style="width: 100%"
            >
              <el-option
                v-for="role in roleList"
                :key="role.rid"
                :label="`${role.rname}${role.remark ? ' - ' + role.remark : ''}`"
                :value="role.rid"
                :disabled="
                  selectedUser.id &&
                  userRoleMap.get(selectedUser.id)?.some((r) => r.rid === role.rid)
                "
              />
            </el-select>
          </el-form-item>
        </el-form>

        <el-alert
          v-if="selectedUser.id && userRoleMap.get(selectedUser.id) && selectedRoleId"
          title="注意：将为用户添加新角色（支持多角色）"
          type="info"
          :closable="false"
          show-icon
          style="margin-top: 10px"
        />
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="userRoleDialogVisible = false">取消</el-button>
          <el-button type="primary" :loading="loading" @click="handleSaveUserRole">
            确定分配
          </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 用户角色详情对话框 -->
    <el-dialog
      v-model="userRoleDetailDialogVisible"
      title="用户角色详情"
      width="600px"
      :close-on-click-modal="false"
    >
      <div v-if="selectedUserForDetail">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="用户名">{{
            selectedUserForDetail.userName
          }}</el-descriptions-item>
          <el-descriptions-item label="用户类型">
            <el-tag :type="selectedUserForDetail.type === 1 ? 'danger' : 'primary'">
              {{ getUserTypeText(selectedUserForDetail.type) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="角色数量" :span="2">
            <el-tag type="info">
              {{
                selectedUserForDetail.id
                  ? userRoleMap.get(selectedUserForDetail.id)?.length || 0
                  : 0
              }}
              个角色
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>

        <el-divider>
          <el-icon><User /></el-icon>
          <span style="margin-left: 8px">角色列表</span>
        </el-divider>

        <div
          v-if="
            selectedUserForDetail.id &&
            userRoleMap.get(selectedUserForDetail.id) &&
            userRoleMap.get(selectedUserForDetail.id)!.length > 0
          "
        >
          <el-table
            :data="userRoleMap.get(selectedUserForDetail.id)"
            style="width: 100%"
            stripe
            size="small"
          >
            <el-table-column prop="rid" label="角色ID" width="80" align="center">
              <template #default="{ row }">
                <el-tag size="small" type="info">#{{ row.rid }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="rname" label="角色名称" min-width="120">
              <template #default="{ row }">
                <div class="role-name">
                  <el-icon class="role-icon"><User /></el-icon>
                  <span>{{ row.rname }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="角色描述" min-width="150">
              <template #default="{ row }">
                <span class="role-remark">{{ row.remark || '暂无描述' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="100" align="center">
              <template #default="{ row }">
                <el-button
                  size="small"
                  type="danger"
                  :icon="Delete"
                  @click="handleRemoveSpecificUserRole(selectedUserForDetail!, row)"
                  plain
                >
                  移除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-empty v-else description="该用户暂无分配角色" />
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="userRoleDetailDialogVisible = false">关闭</el-button>
          <el-button type="primary" :icon="User" @click="handleAssignRole(selectedUserForDetail!)">
            分配新角色
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="css" scoped>
.role-view {
  padding: 24px;
  background: #f5f7fa;
  min-height: calc(100vh - 100px);
}

.page-header {
  margin-bottom: 24px;
}

.page-title {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
  color: #1f2937;
}

.page-description {
  margin: 0;
  color: #6b7280;
  font-size: 14px;
}

.role-card,
.user-role-card {
  margin-bottom: 24px;
  border-radius: 12px;
  border: none;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-icon {
  color: #3b82f6;
  font-size: 20px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
}

.divider-section {
  margin: 32px 0;
}

.search-section {
  margin-bottom: 24px;
  padding: 20px;
  background: #f8fafc;
  border-radius: 8px;
}

.search-form {
  margin: 0;
}

.role-name {
  display: flex;
  align-items: center;
  gap: 8px;
}

.role-icon {
  color: #3b82f6;
  font-size: 16px;
}

.role-remark {
  color: #6b7280;
  font-size: 14px;
}

.user-name {
  display: flex;
  align-items: center;
}

.user-remark {
  color: #6b7280;
  font-size: 14px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 24px;
  padding: 20px 0;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 表格样式优化 */
:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table__header-wrapper) {
  border-radius: 8px 8px 0 0;
}

:deep(.el-table td) {
  border-bottom: 1px solid #f1f5f9;
}

:deep(.el-table tr:hover > td) {
  background-color: #f8fafc !important;
}

/* 按钮样式优化 */
:deep(.el-button--small.is-plain) {
  padding: 5px 12px;
  border-radius: 6px;
}

/* 标签样式优化 */
:deep(.el-tag) {
  border-radius: 6px;
  font-weight: 500;
}

/* 对话框样式优化 */
:deep(.el-dialog) {
  border-radius: 12px;
}

:deep(.el-dialog__header) {
  padding: 24px 24px 12px 24px;
  border-bottom: 1px solid #f1f5f9;
}

:deep(.el-dialog__body) {
  padding: 24px;
}

:deep(.el-dialog__footer) {
  padding: 12px 24px 24px 24px;
  border-top: 1px solid #f1f5f9;
}

/* 描述列表样式优化 */
:deep(.el-descriptions) {
  margin-bottom: 24px;
}

:deep(.el-descriptions__label) {
  font-weight: 600;
  color: #374151;
}

:deep(.el-descriptions__content) {
  color: #6b7280;
}

/* 用户角色显示样式 */
.user-roles {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
  align-items: center;
}

/* 空状态样式优化 */
:deep(.el-empty) {
  padding: 40px 20px;
}

:deep(.el-empty__description) {
  color: #909399;
  font-size: 14px;
}
</style>
